/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */
package cn.knet.sp.ncrm.common.utils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

/**
 * 随机数字工具
 *
 * @author zhangbin
 * @version 2015 /07/10 11:12:38
 */
public class RandomUtils {

	public static void main(String[] args){
		System.err.println(RandomUtils.randomInt(1233));
	}

	enum RandomType {
		CHAR_LOW("charLow"),
		CHAR_UP("charsUp"),
		CHAR_EN("charEn"),
		CHAR_NUMBER("charNumber"),
		CHAR_MARK("charMark"),
		CHAR_ALL("charAll"),
		CHAR_ALLS("charAlls");
		private static final Map<String, String> MAPPING = new LinkedHashMap<String, String>();
		private static final Map<String, String> INVERSE_MAPPING = new LinkedHashMap<String, String>();

		RandomType(String name) {
			this.name = name;
			this.code = name.hashCode();
		}

		private String name;
		private int code;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}
	}

	private static Random random = new Random();
	private static String charsLow = "abcdfeghijklmnopqrstuvwxyz";
	private static String charsNumber = "0123456789";
	private static String charsMark = "~!@#$%^&*()_+{}";

	public static int randomInt(int value) {
		if (value < 1) {
			return random.nextInt();
		} else {
			return random.nextInt(value);
		}
	}

	public static char randomChar(String str) {
		RandomType type = null;
		if (StringUtils.isBlank(str)) {
			type = RandomType.CHAR_ALL;
		}
		int index;
		char result;
		switch (type) {
			case CHAR_LOW:
				index = random.nextInt(charsLow.length());
				result = (charsLow).charAt(index);
			break;
			case CHAR_UP:
				index = random.nextInt(charsLow.length());
				result = (charsLow.toUpperCase()).charAt(index);
			break;
			case CHAR_EN:
				index = random.nextInt(charsLow.length() + charsLow.length());
				result = (charsLow + charsLow.toUpperCase()).charAt(index);
			break;
			case CHAR_NUMBER:
				index = random.nextInt(charsNumber.length());
				result = (charsNumber).charAt(index);
			break;
			case CHAR_MARK:
				index = random.nextInt(charsMark.length());
				result = (charsMark).charAt(index);
				break;
			case CHAR_ALL:
				index = random.nextInt(charsLow.length() + charsLow.length() + charsNumber.length());
				result = (charsLow + charsLow.toUpperCase() + charsNumber).charAt(index);
			break;
			case CHAR_ALLS:
				index = random.nextInt(charsLow.length() + charsLow.length() + charsNumber.length() + charsMark.length());
				result = (charsLow + charsLow.toUpperCase() + charsNumber + charsMark).charAt(index);
			break;
			default:
				index = random.nextInt(str.length());
				result = str.charAt(index);
				break;
		}
		return result;
	}

}
